package com.lxp.tksys.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lxp.tksys.common.BaseContext;
import com.lxp.tksys.common.R;
import com.lxp.tksys.entity.ShoppingCart;
import com.lxp.tksys.entity.User;
import com.lxp.tksys.service.ShoppingCartService;
import com.lxp.tksys.service.UserService;
import com.lxp.tksys.utils.ValidateCodeUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/shoppingCart")  // 用户管理
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;

    @PostMapping("/add")
    private R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart){
        //log.info("购物车数据:{}",shoppingCart);

        //获取用户id，指定哪个用户的购物车
        Long currentId = BaseContext.getCurrentId();
        shoppingCart.setUserId(currentId);

        //查询添加进来的菜品有没有在购物车里面,dishId、setmeal至少有一项有值
        Long dishId = shoppingCart.getDishId();
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,currentId);  //通过userid
        if(dishId!=null){  //加入的是菜品
            queryWrapper.eq(ShoppingCart::getDishId,dishId);

        }else{  //添加的是套餐
            queryWrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }

        //查询是否在购物车中
        ShoppingCart cartServiceOne = shoppingCartService.getOne(queryWrapper);
        if(cartServiceOne!=null){     //在购物车，Number+1
            Integer number = cartServiceOne.getNumber();
            cartServiceOne.setNumber(number+1);
            shoppingCartService.updateById(cartServiceOne);   //更新
        }else {
            shoppingCart.setNumber(1);   //不在购物车，设置number为1
            shoppingCart.setCreateTime(LocalDateTime.now());   //第一次添加的时候时间赋值就行
            shoppingCartService.save(shoppingCart);    //新增
            cartServiceOne=shoppingCart;
        }
        return R.success(cartServiceOne);
    }

    //减少按钮
    @PostMapping("/sub")
    private R<ShoppingCart> sub(@RequestBody ShoppingCart shoppingCart){
        log.info("购物车数据 :{}",shoppingCart);

        //设置用户id，指定哪个用户的购物车
        Long currentId = BaseContext.getCurrentId();
        shoppingCart.setUserId(currentId);
        //查询添加进来的菜品有没有在购物车里面
        Long dishId = shoppingCart.getDishId();
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,currentId);
        if(dishId!=null){  //加入的是菜品
            queryWrapper.eq(ShoppingCart::getDishId,dishId);
        }else{  //添加的是套餐
            queryWrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }

        //查询是否在购物车中
        ShoppingCart cartServiceOne = shoppingCartService.getOne(queryWrapper);

        Integer number = cartServiceOne.getNumber();
        if(number>1) {
            cartServiceOne.setNumber(number - 1);
            shoppingCartService.updateById(cartServiceOne);
        }else{
            shoppingCartService.removeById(cartServiceOne);   //如果为0则直接移除，同时返回一个源对象
            cartServiceOne=shoppingCart;
        }

        return R.success(cartServiceOne);
    }


    //购物车数据
    @GetMapping("/list")
    private R<List<ShoppingCart>> list(){
        log.info("查看购物车 :{}");

        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());    //根据userid来查
        queryWrapper.orderByAsc(ShoppingCart::getCreateTime);
        List<ShoppingCart> list = shoppingCartService.list(queryWrapper);

        return R.success(list);
    }


    @DeleteMapping("/clean")//清空购物车
    private R<String> clean(){

        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());  //根据userid清空
        shoppingCartService.remove(queryWrapper);
        return R.success("清空成功");

    }

}
